package com.hongxun.budget.ui.dialog;

import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.greenentity.sg.CStackPointEntity;
import com.hongxun.greenentity.tk.BDataDictionaryEntity;
import com.hongxun.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 通用单选list dialog
 */

public class ListDialog<T> extends BaseCenterFragmentDialog {
    @BindView(R.id.title)
    TextView titleTv;
    @BindView(R.id.list)
    RecyclerView list;
    String title;
    ResponseListener listener;
    List<T> datas = new ArrayList<>();
    SuperAdapter<T> superAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_list;
    }

    @Override
    protected void initViews() {
        if (!TextUtils.isEmpty(title)) titleTv.setText(title);
        list.setLayoutManager(RecyclerViewUtils.getLinearLayoutManagerForVertical(getActivity()));
        list.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        list.setAdapter(superAdapter = new SuperAdapter<T>(R.layout.content_dialog_list) {
            @Override
            public void bindView(ViewHolder viewHolder, final T t, int i) {
                TextView textView = viewHolder.getView(R.id.content);
                if (t instanceof String) {
                    textView.setText((String) t);
                } else if (t instanceof Integer) {
                    textView.setText(t + "");
                } else if (t instanceof CStackPointEntity) {
                    textView.setText(((CStackPointEntity) t).getSPName());
                } else if (t instanceof BDataDictionaryEntity) {
                    textView.setText(((BDataDictionaryEntity) t).getDValue());
                }
                viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (listener != null) listener.response(t);
                        dismiss();
                    }
                });
            }
        });
        superAdapter.setData(datas);
    }

    public ListDialog setTitle(String title) {
        this.title = title;
        if (titleTv != null) titleTv.setText(title);
        return this;
    }

    public ListDialog setDatas(List<T> datas) {
        this.datas.clear();
        this.datas.addAll(datas);
        if (superAdapter != null) superAdapter.notifyDataSetChanged();
        return this;
    }

    public ListDialog setListener(ResponseListener listener) {
        this.listener = listener;
        return this;
    }

    @OnClick(R.id.cancel)
    public void onViewClicked() {
        dismiss();
    }
}
